import 'package:flutter/material.dart';
import 'package:flutter_soundmagus/page/eq/eq_controller.dart';
import 'package:flutter_soundmagus/util/util.dart';
import 'package:flutter_soundmagus/widget/eq/eq.dart';
import 'package:flutter_soundmagus/widget/vslider.dart';
import 'package:flutter_soundmagus/widget/zfbutton.dart';
import 'package:get/get.dart';

class EqItem extends GetView<EqController> {
  ///频点索引
  final int index;
  const EqItem(this.index, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Widget child = SizedBox(
      width: 40,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Padding(
            padding: const EdgeInsets.only(top: 5),
            child: Obx(() => ZFButton(
                  title: controller.pd.value == index
                      ? controller.freq.toStringAsFixed(0)
                      : controller.eqModel
                          .getCurrentItemsModel()[index]
                          .freq
                          .toStringAsFixed(0),
                  style: TextStyle(fontSize: 10),
                  onPressed: () => controller.updatePd(index),
                  selected: controller.pd.value == index,
                )),
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 5),
            child: ImgBtn(
              'eq_add',
              onTap: () => controller
                ..updatePd(index)
                ..increaseDb(),
            ),
          ),
          Expanded(
            child: SizedBox(
              height: 35,
              child: VSlider(
                style: VSliderStyle.eq,
                value: controller.eqModel.getCurrentItemsModel()[index].ch,
                axis: Axis.vertical,
                min: -20,
                max: 20,
                onChange: (v) {
                  controller
                    ..updatePd(index)
                    ..updateDb(v);
                },
              ),
            ),
          ),
          Padding(
            padding: const EdgeInsets.symmetric(vertical: 5),
            child: ImgBtn(
              'eq_sub',
              onTap: () => controller
                ..updatePd(index)
                ..decreaseDb(),
            ),
          ),
          Padding(
            padding: const EdgeInsets.only(bottom: 10),
            child: Obx(() => ZFButton(
                  title: toDb(controller.db.value),
                  style: TextStyle(fontSize: 10),
                )),
          ),
        ],
      ),
    );
    XoverItemModel xoverItemModel =
        DataMng.shared.xoverModel.items[DataMng.shared.eqModel.selectCH];
    int freq = EQModel.freqs[index];
    //比如左边高通=200，右边低通=15000时，频点<200和频点>15000的禁用
    if (xoverItemModel.high.freq > freq || freq > xoverItemModel.low.freq) {
      //不在xover区间的频点，增加一个遮罩层模拟禁用效果
      child = Stack(
        children: [
          child,
          Container(
            color: Colors.black.withOpacity(0.6),
            width: 40,
          )
        ],
      );
    }

    return child;
  }

  String toDb(double db) =>
      controller.eqModel.getCurrentItemsModel()[index].ch.toStringAsFixed(0);
}
